package com.zyl.blog.common.utils.Extreme;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: Personal_Blog
 * @Package: com.zyl.common.utils.Extreme
 * @ClassName: JsonUtils
 * @Description JSON处理
 * @Author FinalFantasy
 * @Date 2025/2/17-21:17
 */
public class JsonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON序列化失败", e);
        }
    }

    /*将字符串分割逻辑封装成工具方法*/
    public static List<String> splitAndTrim(String source, String delimiter) {
        if (source == null || source.isEmpty()) return Collections.emptyList();
        return Arrays.stream(source.split(delimiter))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList();
    }
}